package cn.springboot.erp.service.impl;

import cn.springboot.erp.commons.QueryParams;
import cn.springboot.erp.commons.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.springboot.erp.entity.Warehouses;
import cn.springboot.erp.service.WarehousesService;
import cn.springboot.erp.mapper.WarehousesMapper;
import org.springframework.stereotype.Service;

/**
* @author asus
* @description 针对表【warehouses】的数据库操作Service实现
* @createDate 2024-05-11 11:56:02
*/
@Service
public class WarehousesServiceImpl extends ServiceImpl<WarehousesMapper, Warehouses>
    implements WarehousesService{

    @Override
    public ResponseResult listPage(QueryParams<Warehouses> queryParams) {
        // 获取分页参数
        Integer page = queryParams.getPage();
        Integer limit = queryParams.getLimit();
        Warehouses searchParams = queryParams.getSearchParams();
        // 分页查询
        Page<Warehouses> warehousesPage = new Page<>(page, limit);
        LambdaQueryWrapper<Warehouses> lambda = new QueryWrapper<Warehouses>().lambda();
        lambda.eq(StringUtils.isNotBlank(searchParams.getName()),Warehouses::getName,searchParams.getName())
                .eq(StringUtils.isNotBlank(searchParams.getManager()),Warehouses::getManager,searchParams.getManager())
                .orderByDesc(Warehouses::getCreateTime);
        baseMapper.selectPage(warehousesPage,lambda);

        return ResponseResult.Success()
                .data("total",warehousesPage.getTotal())
                .data("warehouseList",warehousesPage.getRecords());
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        // 根据id查询
        Warehouses warehouses = baseMapper.selectById(id);
        // 判断仓库状态
        if (warehouses.getState()==0){
            return ResponseResult.Fail().msg("仓库正在使用，暂时不能删除");
        }
        // 删除仓库
        baseMapper.deleteById(id);
        return ResponseResult.Success().msg("删除成功");
    }
}




